﻿using Micro.Extensions.Utilities;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Micro.Extensions.Cache.Redis
{
    public class RedisConfigurator : BaseConfigurator
    {
        private readonly RedisConfig _redisConfig;
        public RedisConfigurator(RedisConfig redisConfig)
        {
            _redisConfig = redisConfig;
        }

        //todo
        //public override void SetCallRecordsLogCenterAddress(string address)
        //{
        //    ArgumentCheck.NotNullOrWhiteSpace(address, nameof(address));
        //    this._redisConfig.CallRecordsLogCenterAddress = address;
        //}

        //todo
        //public override void SetEnableLogCallRecords(bool enable)
        //{
        //    this._redisConfig.EnableLogCallRecords = enable;
        //}

        /// <summary>
        ///设置缓存服务端节点
        /// </summary>
        /// <param name="endpoints">服务端节点集合</param>
        public void SetServerEndPoints(IList<ServerEndPoint> endpoints)
        {
            StringBuilder sb = new StringBuilder();
            endpoints.ToList().ForEach(item => sb.Append($"{item.Host}:{item.Port},"));
            _redisConfig.Endpoints = sb.ToString().TrimEnd(',');
        }

        /// <summary>
        /// 设置缓存服务端节点
        /// </summary>
        /// <param name="serverEndPoints">服务端节点字符串，以逗号隔离，例如：192.168.1.99:6379,192.168.1.100:6379</param>
        public void SetServerEndPoints(string serverEndPoints)
        {
            _redisConfig.Endpoints = serverEndPoints;
        }

        /// <summary>
        /// 设置超时时间(默认为5秒)，以毫秒为单位
        /// </summary>
        public void SetConnectTimeout(int timeout = 5000)
        {
            ArgCheck.NotNegativeOrZero(timeout, nameof(timeout));
            _redisConfig.ConnectTimeout = timeout;
        }

        /// <summary>
        /// 设置是否允许Admin操作
        /// </summary>
        public void SetAllowAdmin(bool allowAdmin)
        {
            _redisConfig.AllowAdmin = allowAdmin;
        }

        /// <summary>
        /// 设置客户端名称（自定义）
        /// </summary>
        public void SetClientName(string clientName)
        {
            ArgCheck.NotNullOrWhiteSpace(clientName, nameof(clientName));
            _redisConfig.ClientName = clientName;
        }

        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="password"></param>
        public void SetPassword(string password)
        {
            //ArgumentCheck.NotNullOrWhiteSpace(password, nameof(password));
            _redisConfig.Password = password;
        }

        /// <summary>
        /// 设置缓存前缀
        /// </summary>
        /// <param name="prefix"></param>
        public void SetPrefix(string prefix)
        {
            ArgCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));
            _redisConfig.Prefix = prefix;
        }

        /// <summary>
        ///  设置发送消息以帮助保持套接字活动的时间, 即连接保持时间（默认60s）,单位秒
        /// </summary>
        public void SetKeepAlive(int keepAlive = 60)
        {
            ArgCheck.NotNegativeOrZero(keepAlive, nameof(keepAlive));
            _redisConfig.KeepAlive = keepAlive;
        }

        /// <summary>
        /// 指定系统允许操作的时间(默认为1秒)，以毫秒为单位
        /// </summary>
        /// <param name="timeout"></param>
        public void SetTimeOut(int timeout = 1000)
        {
            ArgCheck.NotNegativeOrZero(timeout, nameof(timeout));
            _redisConfig.SyncTimeout = timeout;
        }
    }
}